<?php

class MActiveRecord extends CActiveRecord {

    public $DateTimeServer;

    public function __construct($scenario = 'insert') {
        $this->DateTimeServer = new DateTimeServer();
        parent::__construct($scenario);
    }

    public function __get($name) {
        $type = isset($this->getMetaData()->tableSchema->columns[$name]) ? $this->getMetaData()->tableSchema->columns[$name]->dbType : "";

        $return = parent::__get($name);
        if ($return) { // an actual value has been set
            $dtp = Yii::app()->dateFormatter;
            if ($type == "date")
                return $dtp->formatDateTime(CDateTimeParser::parse($return, 'dd/MM/yyyy'), 'medium', null);
            else if ($type == "datetime")
                return $dtp->formatDateTime(CDateTimeParser::parse($return, 'dd/MM/yyyy hh:mm:ss'));
        }

        return $return;
    }

    public function __set($name, $value) {
        $type = isset($this->getMetaData()->tableSchema->columns[$name]) ? $this->getMetaData()->tableSchema->columns[$name]->dbType : "";
        if ($type == "date")
            parent::__set($name, date("Y-m-d", strtotime($value)));
        elseif ($type == "datetime")
            parent::__set($name, date("Y-m-d H:i:s", strtotime($value)));
        else
            parent::__set($name, $value);
    }

}

?>
